package mo.tags.reservoirsample;

import mo.common.ListNode;

import java.util.Random;

public class L382 {

    private ListNode head;
    private Random random;


    public L382(ListNode head) {
        this.head = head;
        this.random = new Random();
    }

    public int getRandom() {
        int i = 1, ans = 0;
        for (ListNode tmp = head; tmp != null; tmp = tmp.next) {
            if (random.nextInt(i) == 0) {
                ans = tmp.val;
            }
            i++;
        }
        return ans;
    }

}
